import helpers.Client;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import protocol.registration.ClientRegistrationRequest;
import protocol.registration.ClientRegistrationResponse;

public class TCPServer extends Server{

	public static final int MaxAccepts = 3;

	// folosite pentru a trimite mesaje cu identificator; inca nu l-am pus si pe asta
	public static final String startClient = "Client";
	public static final String stopClient = "ClientStop";
	
	private ServerSocket serverSocketClient;
	private ServerSocket serverSocketPrinter;
	private int portC;
	private int portP;
	
	private String received;
	
	
	public TCPServer(int portClient, int portPrinter) {
		this.portC = portClient;
		this.portP = portPrinter;
	}
	
	@Override
	public void run()
	{
		try {
			
			this.serverSocketClient = new ServerSocket(portC);
			this.serverSocketPrinter = new ServerSocket(portP);
		} catch (Exception e) {
			
			System.err.println("Exceptie la crearea ServerSocket-ului: " + e);
			e.printStackTrace();
			return;
		}
		
		while(true)
		{
		
		for (int i = 0; i < MaxAccepts; i++) {
			
			System.out.println("[TCPServer] Accepting a new connection");

			Socket clientSocket = null;
			Socket printerSocket = null;
			
			try {
				
				clientSocket = this.serverSocketClient.accept();
				//printerSocket = this.serverSocketPrinter.accept();
			} catch (Exception e) {
				
				System.err.println("Eroare la accept: " + e);
				e.printStackTrace();
			}
			// Primeste mesaj de la Client si trimite ACK
			try {
				
				System.out.println("Astept Cerere");
				ClientRegistrationRequest crr = new ClientRegistrationRequest();
				crr.read(new DataInputStream(clientSocket.getInputStream()));
				System.out.println("Am primit cerere de client");
				Client client = new Client(crr.getID(),crr.getType(),crr.getIp(),crr.getPort(),crr.getZona());
				//generez port nou pentru client , il adaug in lista
				m_clients.add(client);
				
				TCPClientServer tcps = new TCPClientServer(++ports , client , this);
				m_thClients.add(tcps);
				tcps.start();
				
				System.out.println("Trimit raspuns");
				ClientRegistrationResponse cresp  = new ClientRegistrationResponse(client.getIp() ,ports,client.getId());
				cresp.write(new DataOutputStream(clientSocket.getOutputStream()));
							
				clientSocket.close();
			} catch (Exception e) {
				
				System.err.println("Eroare la raspuns Server-Client: " + e);
				e.printStackTrace();
			}
			finally
			{
				try {
					clientSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}		
		
		
			
		}
		
	/*	try {
			
			this.serverSocketClient.close();
			this.serverSocketPrinter.close();
		} catch (Exception e) {
			
			System.err.println("ServerSocket close exception: " + e);
			e.printStackTrace();
		}*/
	}
}
